#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem 
// 
// Project : Skomart.Update.Download 
// 
// File : CompareConfigManager.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 比较客户端服务器端更新状态文件
// 
// History : 
//			2010-08-03 11:27:31	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
#endregion

namespace Skomart.Update.Download
{
    /// <summary> 
    /// 比较客户端服务器端更新状态文件
    /// </summary>
    internal class CompareConfigManager
    {
        /// <summary>
        /// 比较获取需要下载的文件
        /// </summary>
        /// <param name="serverItems">更新服务器上的文件信息</param>
        /// <param name="clientItems">客户端文件信息</param>
        /// <returns>需下载的文件信息</returns>
        public static IList<HasUpdateFileInfo> GetHasUpdateFiles(UpdateConfigItem[] serverItems, IList<ClientFileConfigItem> clientItems)
        {
            IList<HasUpdateFileInfo> hasUpdateFileInfos = new List<HasUpdateFileInfo>();

            foreach (UpdateConfigItem serverItem in serverItems)
            {
                ClientFileConfigItem clientItem = GetClientItem(clientItems, serverItem.FileName);
                if (clientItem == null)
                {
                    //Add File 存在于服务端存在于客户端
                    HasUpdateFileInfo hasUpdateFileInfo = new HasUpdateFileInfo
                                                              {
                                                                  File = serverItem.FileName,
                                                                  OperateType = OperateType.Add,
                                                                  UpdateValue = serverItem.NewestValue,
                                                                  FileLength = serverItem.Length
                                                              };
                    hasUpdateFileInfos.Add(hasUpdateFileInfo);
                }
                else
                {
                    //UpdateFile 同时存在于服务端和客户端
                    if (serverItem.CompareType == CompareType.LastUpdateDate)
                    {
                        DateTime clientDate = JackieConvert.ToDateTime(clientItem.LastestValue);
                        DateTime serverDate = JackieConvert.ToDateTime(serverItem.NewestValue);
                        if (serverDate > clientDate)
                        {
                            HasUpdateFileInfo hasUpdateFileInfo = new HasUpdateFileInfo
                                                                      {
                                                                          File = serverItem.FileName,
                                                                          OperateType = OperateType.Update,
                                                                          UpdateValue = serverItem.NewestValue,
                                                                          FileLength = serverItem.Length
                                                                      };
                            hasUpdateFileInfos.Add(hasUpdateFileInfo);
                        }
                    }
                    ///TODO:实现按版本比较更新信息
                }
            }

            //Delete 存在于客户端，服务端不存在
            foreach (ClientFileConfigItem clientItem in clientItems)
            {
                UpdateConfigItem serverItem = GetServerItem(serverItems, clientItem.File);
                if (serverItem == null)
                {
                    HasUpdateFileInfo hasUpdateFileInfo = new HasUpdateFileInfo
                    {
                        File = clientItem.File,
                        OperateType = OperateType.Delete,
                        UpdateValue = clientItem.LastestValue
                    };
                    hasUpdateFileInfos.Add(hasUpdateFileInfo);
                }
            }
            return hasUpdateFileInfos;
        }

        private static UpdateConfigItem GetServerItem(UpdateConfigItem[] serverItems, string file)
        {
            foreach (UpdateConfigItem serverItem in serverItems)
            {
                if (serverItem.FileName == file)
                {
                    return serverItem;
                }
            }
            return null;
        }

        private static ClientFileConfigItem GetClientItem(IList<ClientFileConfigItem> clientItems, string file)
        {
            foreach (ClientFileConfigItem item in clientItems)
            {
                if (item.File == file)
                {
                    return item;
                }
            }
            return null;
        }
    }
}